package top.carrotguo.weibo.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.logging.Logger;

import butterknife.BindInt;
import butterknife.BindView;
import butterknife.ButterKnife;
import top.carrotguo.weibo.R;
import top.carrotguo.weibo.bean.CommentListBean;
import top.carrotguo.weibo.utils.DateUtils;
import top.carrotguo.weibo.utils.StringUtils;
import top.carrotguo.weibo.utils.UIUtils;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentHolder> {

    private ArrayList<CommentListBean.CommentsBean> datas;

    public CommentAdapter(ArrayList<CommentListBean.CommentsBean> datas){
        this.datas = datas;
    }

    @NonNull
    @Override
    public CommentHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater
                .from(viewGroup.getContext())
                .inflate(R.layout.item_comment,viewGroup,false);
        return new CommentHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CommentHolder commentHolder, int i) {
        //用户名
        commentHolder.tvName.setText(datas.get(i).getUser().getName());

        //头像
        Glide.with(UIUtils.getContext())
                .load(datas.get(i).getUser().getAvatar_large())
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(commentHolder.ivHead);

        //评论内容
        commentHolder.tvContent.setText(
                StringUtils.getContentText(UIUtils.getContext(),
                        commentHolder.tvContent,
                        datas.get(i).getText()));

        //评论时间
        commentHolder.tvCreateTime.setText(DateUtils.getLongTime(datas.get(i).getCreated_at()));
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class CommentHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.iv_head)
        ImageView ivHead;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.iv_photo)
        ImageView ivPhoto;
        @BindView(R.id.create_time)
        TextView tvCreateTime;

        public CommentHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
